package com.aiatomic.couting;

import java.io.IOException;
import java.io.InputStream;

import com.aiatomic.couting.models.TemplateEntity;
import com.aiatomic.couting.scan.FragmentScanBegin;
import com.aiatomic.couting.util.AppUtils;
import com.aiatomic.couting.widgets.ROIView;
import com.aiatomic.couting.widgets.TaggableImageView;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import androidx.drawerlayout.widget.DrawerLayout;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class ScanActivity extends Activity {
	public DrawerLayout dl;
	public View lv;
	public TaggableImageView scanImg;
	public ROIView frameROI;
	private TemplateEntity curTpl;
	public TextView txtCount;
	public View txtCountContainer;
	public TemplateEntity getCurTpl() {
		return curTpl;
	}

	public void setCurTpl(TemplateEntity curTpl) {
		this.curTpl = curTpl;
	}

	public TaggableImageView getScanImg() {
		return scanImg;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_scan);
		this.findViewById(R.id.topBar).setVisibility(android.view.View.VISIBLE);
		txtCount=(TextView) this.findViewById(R.id.count_txt);
		txtCountContainer=this.findViewById(R.id.count_txt_container);
		txtCountContainer.setVisibility(android.view.View.GONE);
		scanImg = (TaggableImageView) this.findViewById(R.id.scanImgId);
		frameROI=(ROIView)this.findViewById(R.id.frameROI);
		Intent intent = getIntent();
		curTpl=(TemplateEntity) intent.getSerializableExtra("template");
		Uri uri = intent.getData();
		if (uri != null) {
			InputStream input;
			try {
				input = this.getContentResolver().openInputStream(uri);
				BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options();
				onlyBoundsOptions.inSampleSize=2;
				onlyBoundsOptions.inPreferredConfig=Bitmap.Config.RGB_565;
				Bitmap src = BitmapFactory.decodeStream(input, null, onlyBoundsOptions);
				input.close();
				scanImg.setImageBitmap(src);
				/*if(src!=null&&!src.isRecycled()){
					src.recycle();
				}*/
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
		this.findViewById(R.id.drawer_layout);
		lv = (View) findViewById(R.id.right_drawer);
		dl = (DrawerLayout) findViewById(R.id.drawer_layout);
		FragmentScanBegin scanBegin = new FragmentScanBegin();

		addFragment(scanBegin);
		bindEvent();
	}

	private void bindEvent() {
		TextView txtBack = (TextView) this.findViewById(R.id.txtBack);
		txtBack.setText(R.string.back);
		txtBack.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		});
		TextView txtNext = (TextView) this.findViewById(R.id.txtNext);
		txtNext.setText(R.string.main);
		txtNext.setVisibility(View.VISIBLE);
		txtNext.setCompoundDrawables(null, null, null, null);
		txtNext.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});

		TextView txtMiddle = (TextView) this.findViewById(R.id.txtMiddle);
		txtMiddle.setText(R.string.app_name);

		bindDrawerEvent();
	}

	private void bindDrawerEvent() {
		this.findViewById(R.id.drawer_close_layout).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dl.closeDrawer(lv);
			}
		});
		this.findViewById(R.id.drawer_account).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (!AppUtils.isLogin(ScanActivity.this)) {
					Intent intent = new Intent(ScanActivity.this, LoginActivity.class);
					ScanActivity.this.startActivity(intent);
				} else {
					Intent intent = new Intent(ScanActivity.this, AccountInfoActivity.class);
					intent.setAction(AccountInfoActivity.ACTION_MAIN);
					ScanActivity.this.startActivity(intent);
				}
			}
		});

		this.findViewById(R.id.drawer_about).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(ScanActivity.this, AboutMeActivity.class);
				ScanActivity.this.startActivity(intent);
			}
		});

		this.findViewById(R.id.drawer_buy).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
//				if (!AppUtils.isLogin(ScanActivity.this)) {
//					Intent intent = new Intent(ScanActivity.this, LoginActivity.class);
//					ScanActivity.this.startActivity(intent);
//				} else {
//					Intent intent = new Intent(ScanActivity.this, AccountInfoActivity.class);
//					intent.setAction(AccountInfoActivity.ACTION_SHOP);
//					ScanActivity.this.startActivity(intent);
//				}
			}
		});
		this.findViewById(R.id.drawer_feedback).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
//				Intent intent = new Intent(ScanActivity.this, AccountInfoActivity.class);
//				intent.setAction(AccountInfoActivity.ACTION_FEEDBACK);
//				ScanActivity.this.startActivity(intent);
			}
		});
		this.findViewById(R.id.drawer_setting).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(ScanActivity.this, SettingActivity.class);
				ScanActivity.this.startActivity(intent);
			}
		});
	}

	public void addFragment(Fragment fragment) {
		FragmentManager fm = getFragmentManager();
		FragmentTransaction transaction = fm.beginTransaction();
		transaction.replace(R.id.id_content, fragment);
		transaction.addToBackStack(null);
		transaction.commit();
	}

	@Override

	public void onBackPressed() {

		FragmentManager fm = getFragmentManager();

		if (fm.getBackStackEntryCount() > 1) {

			fm.popBackStackImmediate();

		} else {
			this.finish();
		}

	}
}

